Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support basePath for relative href prop #248

Merged
merged 6 commits into from
Feb 3, 2025
Merged

Conversation

upreeti
Copy link
Contributor

@upreeti upreeti commented Dec 16, 2024

We are using the basePath prop for navigation in our application and for our side navigation we are using the MenuLinkItem which takes an href prop. Because of this, we did not see the basePath being added to the route when hovering over the side navigation.

I found this PR which prefixes basePath when using the to prop, and I was wondering if this could be extended to relative hrefs as well. We added a patch fix for this in our code, but I created this PR to upstream this change of prefixing to relative href paths, after reaching out to @pancaspe87

Copy link

changeset-bot bot commented Dec 16, 2024

🦋 Changeset detected

Latest commit: 4c10b28

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
react-resource-router Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@upreeti upreeti changed the title Support basePath for href prop Support basePath for relative href prop Dec 16, 2024
@@ -72,8 +72,12 @@ const Link = forwardRef<HTMLButtonElement | HTMLAnchorElement, LinkProps>(
)) ||
''
: to;
const IS_ABSOLUTE_LINK_REGEX = /^((?:(http|https):\/\/)|\/\/)/;
Copy link
Collaborator

@liamqma liamqma Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code change itself looks good. However, I wonder if href should be prefixed.
For to, it makes sense because to must be one of routes.
But for href, it's basically any URL. I know that you have the check whether href is a relative URL. If yes, then prefix. But should we prefix it at the product code?

const { getBasePath } = useRouterActions();
const basePath = getBasePath();

const href = `${getBasePath}/something`;

<Link href={href} />

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it too many places we need to prefix if it's done at the product level?

Copy link
Contributor Author

@upreeti upreeti Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that's why we wanted to have it built into the code here since it would be more scalable and if we did have it at the product level there would be too many places to prefix. We are also using the MenuLinkItem component (which uses Link downstream), and it doesn't take a to prop (only href). Also, it seems more intuitive that once the basePath prop is passed in, it will apply to both the to prop and href as opposed to having to define it for href again

we have this change as a patch fix right now, but I thought it might be useful to upstream

@liamqma liamqma merged commit 7d56bc3 into master Feb 3, 2025
3 checks passed
@liamqma liamqma deleted the prefix-basePath-for-href branch February 3, 2025 23:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants